﻿using System;
using System.Web.Mvc;
using NotifyPilot.WCF.Helpers;

namespace NotifyPilot.Controllers
{
    public partial class ConfigController : Controller
    {
        [HttpGet]
        public ActionResult Yammer()
        {
            return View(YammerConfigParametersFactory.LoadParameters());
        }

        [HttpPost]
        public ActionResult Yammer(YammerConfigParameters model)
        {
            if (ModelState.IsValid)
            {
                if (model.Save())
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }

        [HttpGet]
        public virtual ActionResult YammerAuthorization()
        {
            // Redirect to the yammer authorize screen to obtain an authorise code
            var config = YammerConfigParametersFactory.LoadParameters();
            if (config.HaveRequiredParameters)
            {
                // ReSharper disable once PossibleNullReferenceException
                var root = this.Request.Url.AbsoluteUri.Replace(this.Request.Url.AbsolutePath, String.Empty) + '/';
                var url =
                    YammerConfigParametersFactory.CreateYammerClient(config)
                        .GetLoginLinkUri(root + Url.Action("YammerAuthorizedCode", "Config"));
                this.TempData["YammerComeFrom"] = "config";
                return Redirect(url);
            }
            return RedirectToAction("Yammer", "Config");
        }

        public ActionResult YammerAuthorizedCode(String code)
        {
            // Back from Yammer OAUth2 with Authorization Code
            if (!String.IsNullOrWhiteSpace(code))
            {
                var config = YammerConfigParametersFactory.LoadParameters();
                var yammerClient = YammerConfigParametersFactory.CreateYammerClient(config);
                switch ((string)this.TempData["YammerComeFrom"])
                {
                    case "user": // authorized from user screen
                    //yammerService.SaveToken(this.CurrentUserDB.userid, code);
                    //return RedirectToAction("UserView", "User", new { id = this.CurrentUserDB.userid });
                    case "config": // authorized from config (NtifyPilot actor)
                        // set the authorization code & Ask the token from this user/code
                        yammerClient.SetAuthorizationCode(code);
                        config.Code = code;
                        config.Token = yammerClient.GetToken();
                        config.Save();
                        return RedirectToAction("Yammer", "Config");
                }
            }
            return null;
        }

        public ActionResult YammerRenewToken()
        {
            //var yammerService = new YammerServices();
            //yammerService.SaveYammerAdminToken(null);
            return RedirectToAction("Yammer", "Config");
        }

        public ActionResult TestPostOnYammer()
        {
            var config = YammerConfigParametersFactory.LoadParameters();
            if (config.IsProballyReadyToPost)
            {
                var yammerClient = YammerConfigParametersFactory.CreateYammerClient(config);
                yammerClient.PostMessage("NotifyPilot : I'm alive", config.PublishGroup, null);
            }
            return RedirectToAction("Index", "Home");

        }

    }
}